package com.eastday.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

public class ExceptionHandler {
	
	private Exception exception;
	private Logger logger;
	private SimpleLayout simpleLayout;
	
	public ExceptionHandler()
	{
		logger = Logger.getRootLogger();
		simpleLayout = new SimpleLayout();
	}

	public Exception getException() {
		return exception;
	}

	public void setException(Exception exception) {
		this.exception = exception;
	}

	public void displayOnConsole(String ex){
		ConsoleAppender consoleAppender = new ConsoleAppender(simpleLayout);
		logger.addAppender(consoleAppender);
		logger.setLevel((Level)Level.INFO);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		logger.info(ex+ "  "+formatter.format(new Date()));
	}
	
	public void writeToLog(String ex){
		try {
			FileAppender fileAppender = new FileAppender(simpleLayout,ParseConfigFactory.sysConfigModel.getLogfile(),false);
			logger.addAppender(fileAppender);
			logger.setLevel((Level)Level.WARN);
			logger.warn(ex);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
		}
	}
	
	

}
